package com.qianfeng.zw.meilishuo.adapter;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.qianfeng.zw.meilishuo.R;
import com.qianfeng.zw.meilishuo.utils.ImageLoaderHelper;
import com.qianfeng.zw.meilishuo.widget.RecyclingPagerAdapter;

import java.util.List;

/**
 * @作 用:
 * @创 建 人: zhangwei
 * @日 期: 15/4/4 17:17
 * @修 改 人:
 * @日 期:
 */
public class FashBannerAdapter extends RecyclingPagerAdapter implements ViewPager.OnPageChangeListener {
    private Context context;
    private List<String> imageUrls;
    //是否无限循环
    private boolean isInfiniteLoop;
    private DisplayImageOptions options;
    private ViewGroup dotGroup;
    private int currentIndex;

    public FashBannerAdapter(Context context, ViewGroup dotGroup, List<String> imageUrls) {
        this.dotGroup = dotGroup;
        this.context = context;
        this.imageUrls = imageUrls;
        isInfiniteLoop = false;
        options = ImageLoaderHelper.getInstance().getDefaultPicDisplayImageOptions();
    }


    @Override
    public int getCount() {
        return isInfiniteLoop ? Integer.MAX_VALUE : imageUrls.size();
    }

    /**
     * get really position
     *
     * @param position
     * @return
     */
    private int getPosition(int position) {
        return isInfiniteLoop ? position % imageUrls.size() : position;
    }

    @Override
    public View getView(int position, View view, ViewGroup container) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            holder.imageView = new ImageView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            holder.imageView.setLayoutParams(params);
            holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            view = holder.imageView;
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        ImageLoader.getInstance().displayImage(imageUrls.get(getPosition(position)), holder.imageView, options);
        currentIndex = position;
        return view;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < dotGroup.getChildCount(); i++) {
            if (i == getPosition(position)) {
                ((ImageView) dotGroup.getChildAt(i)).setImageResource(R.mipmap.dot_press);
            } else {
                ((ImageView) dotGroup.getChildAt(i)).setImageResource(R.mipmap.dot_nomal);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private static class ViewHolder {
        ImageView imageView;
    }

    /**
     * @return the isInfiniteLoop
     */
    public boolean isInfiniteLoop() {
        return isInfiniteLoop;
    }

    /**
     * @param isInfiniteLoop the isInfiniteLoop to set
     */
    public FashBannerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
        this.isInfiniteLoop = isInfiniteLoop;
        return this;
    }

    /**
     * 刷新数据
     *
     * @param
     */
    public void refreshView() {
        addDotView();
        isInfiniteLoop = true;
        this.notifyDataSetChanged();
    }

    /**
     * 添加圆点
     */
    private void addDotView() {
        Log.e("count", imageUrls.size() + "");
        if (imageUrls != null && !imageUrls.isEmpty()) {
            for (int i = 0; i < imageUrls.size(); i++) {
                ImageView dotView = new ImageView(context);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                params.leftMargin = 5;
                dotView.setLayoutParams(params);
                if (i == 0) {
                    dotView.setImageResource(R.mipmap.dot_press);
                } else {
                    dotView.setImageResource(R.mipmap.dot_nomal);
                }
                dotGroup.addView(dotView);
            }

        }
    }
}